package com.cykj.service.Impl;

import com.cykj.dto.ResponseDto;
import com.cykj.mapper.UserMapper;
import com.cykj.pojo.PsUser;
import com.cykj.service.UserService;
import com.cykj.vo.QueryUser;
import com.cykj.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: Psychology
 * @BelongsPackage: com.cykj.service.Impl
 * @Author: 小林
 * @CreateTime: 2025-01-17 20:32
 * @Description: 用户表service
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;

    /**
     * 获取所有数据
     * @return
     */
    @Override
    public ResponseDto getAll(QueryUser vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<PsUser> users = mapper.getAll(vo);
        PageInfo<PsUser> page = new PageInfo<>(users);
        return ResponseDto.success(page);
    }

    /**
     * 获取用户id
     * @param id
     * @return
     */
    @Override
    public PsUser getUserById(Integer id) {
        return mapper.getId(id);
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseDto updateUserStatus(Integer id, Integer status) {
        int result = mapper.updateUserStatus(id, status);
        if(result>0){
            return ResponseDto.success("修改成功");
        }
        return ResponseDto.error("修改失败");
    }

    /**
     * 后台用户重置密码
     * @param id
     * @param newPassword
     * @return
     */
    @Override
    public ResponseDto resetPassword(Integer id, String newPassword) {
        int i = mapper.resetPassword(id, newPassword);
        if(i>0){
            return ResponseDto.success("密码重置成功!");
        }
        return ResponseDto.error("密码重置失败");
    }

    /**
     * 软删除
     * @param id
     * @param userDelete
     * @return
     */
    @Override
    public ResponseDto updateUserDelete(Integer id, Integer userDelete) {
        int i = mapper.userDelete(id, userDelete);
        if(i>0){
            return ResponseDto.success("软删除成功");
        }
        return ResponseDto.error("软删除失败");
    }

    // 获取七天内的用户注册量
    @Override
    public ResponseDto getRegisterCountInSevenDays() {
        // 获取七天内的用户注册量（按天统计）
        List<Map<String, Object>> registerCountList = mapper.getRegisterCountInSevenDays();

        // 将查询结果转换为 Map<String, Integer> 格式
        Map<String, Integer> registerCountMap = new LinkedHashMap<>();
        int totalCount = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (Map<String, Object> row : registerCountList) {
            Date registerDate = (Date) row.get("registerDate");
            String registerDateStr = sdf.format(registerDate);
            Integer registerCount = ((Long) row.get("registerCount")).intValue(); // COUNT(*) 返回的是 Long 类型
            registerCountMap.put(registerDateStr, registerCount);
            totalCount += registerCount;
        }

        // 将总用户新增数放入返回结果中
        Map<String, Object> result = new HashMap<>();
        result.put("dailyCount", registerCountMap);
        result.put("totalCount", totalCount);

        return ResponseDto.success(result);
    }


    // 获取本月每周的用户注册量
    @Override
    public ResponseDto getRegisterCountInOneMonth() {
        // 获取本月每周的用户注册量
        List<Map<String, Object>> registerCountList = mapper.getRegisterCountInOneMonthByWeek();

        // 将查询结果转换为 Map<String, Integer> 格式
        Map<String, Integer> registerCountMap = new LinkedHashMap<>();
        int totalCount = 0;
        for (Map<String, Object> row : registerCountList) {
            String weekOfMonth = "第" + row.get("weekOfMonth") + "周";
            Integer registerCount = ((Long) row.get("registerCount")).intValue();
            registerCountMap.put(weekOfMonth, registerCount);
            totalCount += registerCount;
        }

        // 将总用户新增数放入返回结果中
        Map<String, Object> result = new HashMap<>();
        result.put("weeklyCount", registerCountMap);
        result.put("totalCount", totalCount);

        return ResponseDto.success(result);
    }

    // 获取近半年每月的用户注册量
    @Override
    public ResponseDto getRegisterCountInHalfYear() {
        // 获取近半年每月的用户注册量
        List<Map<String, Object>> registerCountList = mapper.getRegisterCountInHalfYearByMonth();

        // 将查询结果转换为 Map<String, Integer> 格式
        Map<String, Integer> registerCountMap = new LinkedHashMap<>();
        int totalCount = 0;
        for (Map<String, Object> row : registerCountList) {
            String month = (String) row.get("month");
            Integer registerCount = ((Long) row.get("registerCount")).intValue();
            registerCountMap.put(month, registerCount);
            totalCount += registerCount;
        }

        // 将总用户新增数放入返回结果中
        Map<String, Object> result = new HashMap<>();
        result.put("monthlyCount", registerCountMap);
        result.put("totalCount", totalCount);

        return ResponseDto.success(result);
    }

    /**
     * 用户扣费
     * @param vo
     * @return
     */
    @Override
    public ResponseDto deductions(UserVo vo) {
        int i = mapper.deductions(vo);
        if (i > 0) {
            return ResponseDto.success("扣除成功！");
        }
        return ResponseDto.error("扣除失败");
    }

    /**
     * @param vo
     * @return
     */
    @Override
    public ResponseDto addMoney(UserVo vo) {
        int i = mapper.addMoney(vo);
        if (i > 0) {
            return ResponseDto.success("添加成功！");
        }
        return ResponseDto.error("添加失败");
    }

    /**
     * 查询对应用户id的余额
     * @param vo
     * @return
     */
    @Override
    public ResponseDto selectUserBalance(UserVo vo) {
        PsUser user =  mapper.selectUserBalance(vo);
        return user!=null? ResponseDto.success(user):ResponseDto.error("查询失败");
    }
}

